"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var api_1 = require("../../../config/api");
var local_storage_service_1 = require("../../lib/service/local-storage.service");
var address_service_1 = require("../../lib/service/address.service");
var AccountInfo = /** @class */ (function () {
    function AccountInfo() {
    }
    return AccountInfo;
}());
exports.AccountInfo = AccountInfo;
var AccountExtend = /** @class */ (function () {
    function AccountExtend() {
    }
    return AccountExtend;
}());
exports.AccountExtend = AccountExtend;
var PasswordInfo = /** @class */ (function () {
    function PasswordInfo() {
    }
    return PasswordInfo;
}());
exports.PasswordInfo = PasswordInfo;
var MainHeaderComponent = /** @class */ (function () {
    function MainHeaderComponent(httpService, router, localStorageService, addressService, adOssUploadService) {
        this.httpService = httpService;
        this.router = router;
        this.localStorageService = localStorageService;
        this.addressService = addressService;
        this.adOssUploadService = adOssUploadService;
        this.isShow = false;
        this.aliyunOssOutputUrl = api_1.AliyunOssOutputUrl;
        this.provinceList = [];
        this.cityList = [];
        this.regionList = [];
        this.provinceCode = 0; // 选中省code
        this.cityCode = 0;
        this.regionCode = 0;
        this.authorizetion_status = false;
        this.publish_status = false;
        this.hasGetStatus = false;
        this.hasChangedImg = false;
        this.modifiedType = 'account'; // 修改账户的类型
        this.accountClass = [];
        this.firClass = [];
        this.secClass = [];
        this.fir = 0;
        this.sec = 0;
        this.accountInfo = new AccountInfo();
        this.passwordInfo = new PasswordInfo();
        this.accountExtend = new AccountExtend();
        this.canClick = true;
    }
    MainHeaderComponent.prototype.ngOnInit = function () {
        this.head_img = this.localStorageService.getUserInfo().avatar;
        this.hadLoginName = this.localStorageService.getUserInfo().nick_name;
    };
    MainHeaderComponent.prototype.ngDoCheck = function () {
        if (this.hasHeaderAdditional) {
            this.appletName = this.localStorageService.getNowApplet().title;
        }
    };
    // 获取当前小程序的状态
    MainHeaderComponent.prototype.getAuthStatus = function () {
        var authorization = this.localStorageService.getNowApplet();
        if (authorization && !this.hasGetStatus) {
            if (authorization.status === '未授权') {
                this.authorizetion_status = false;
                this.publish_status = false;
            }
            else if (authorization.status === '未发布') {
                this.authorizetion_status = true;
                this.publish_status = false;
            }
            else {
                this.authorizetion_status = true;
                this.publish_status = true;
            }
        }
    };
    MainHeaderComponent.prototype.clear = function () {
        this.localStorageService.clearAll();
        location.href = '/';
    };
    MainHeaderComponent.prototype.accountDlg = function () {
        if (!this.localStorageService.getNowApplet()) {
            return;
        }
        var userInfo = this.localStorageService.getUserInfo();
        this.accountInfo.nick_name = userInfo.nick_name;
        this.accountInfo.sex = userInfo.sex;
        this.accountInfo.avatar = userInfo.avatar;
        // this.accountInfo.address_code = userInfo.address_code;
        // this.accountInfo.address_code_explain = userInfo.address_code_explain;
        // this.accountInfo.address_detail = userInfo.address_detail;
        if (!!userInfo.avatar) {
            this.img_url = api_1.AliyunOssOutputUrl + '/' + userInfo.avatar;
        }
        // this.getAddressDetail(userInfo.address_code);
        this.getAccountExtend();
        // this.personModal.show();
        this.router.navigate(['/member/account/user']);
    };
    // 上传图片
    MainHeaderComponent.prototype.uploadImg = function ($event) {
        this.hasChangedImg = true;
        var file = $event.path[0].files[0];
        var myReader = new FileReader();
        var self = this;
        this.img_file = file;
        myReader.readAsDataURL(file);
        myReader.onloadend = function (e) {
            self.img_url = myReader.result;
        };
        var suffix = file.name.replace(/.+\./, '').toLowerCase();
        var now = new Date();
        this.img_name = now.getTime() + '.' + suffix;
    };
    // 获取用户详情
    MainHeaderComponent.prototype.getUserInfo = function () {
        var _this = this;
        var url = api_1.adSystemApiUrl + "/v1/member/account/user_infomation";
        return this.httpService.httpGetAuth(url).subscribe(function (res) {
            if (res.code === 2000) {
                _this.localStorageService.setUserInfo(res.data);
                _this.hadLoginName = _this.localStorageService.getUserInfo().nick_name;
                _this.head_img = _this.localStorageService.getUserInfo().avatar;
            }
            else {
                _this.type = 'error';
                _this.message = res.message;
                _this.isShow = true;
            }
        });
    };
    // 获取用户拓展信息
    MainHeaderComponent.prototype.getAccountExtend = function () {
        var _this = this;
        var url = api_1.adSystemApiUrl + "/v1/member/account/getAccountExtend";
        return this.httpService.httpGetAuth(url).subscribe(function (res) {
            if (res.code === 2000) {
                _this.accountExtend.company_name = res.data.company_name;
                _this.accountExtend.company_address_code = res.data.company_address_code;
                _this.accountExtend.company_address_detail = res.data.company_address_detail;
                _this.accountExtend.contacts = res.data.contacts;
                _this.accountExtend.contacts_mobile = res.data.contacts_mobile;
                _this.accountExtend.contacts_email = res.data.contacts_email;
                _this.getAddressDetail(_this.accountExtend.company_address_code);
                _this.getAllClass(0, 'firClass');
                if (!!res.data.class.length) {
                    _this.accountClass = [];
                    res.data.class.every(function (val, idx, array) {
                        _this.accountClass.push({ firId: val.frist_class.id, secId: val.second_class.id, firName: val.frist_class.class_name, secName: val.second_class.class_name });
                        return true;
                    });
                }
            }
            else {
                _this.type = 'error';
                _this.message = res.message;
                _this.isShow = true;
            }
        });
    };
    // 获取用户分类扩展信息
    MainHeaderComponent.prototype.getAllClass = function (parent, container) {
        var _this = this;
        var parent_id;
        if (!parent) {
            parent_id = 0;
        }
        else {
            var idx = parent.indexOf('&');
            parent_id = this.fir.substring(0, idx);
        }
        var url = api_1.adSystemApiUrl + "/v1/member/account/getAllClass?parent_id=" + parent_id;
        return this.httpService.httpPostAuth(url, '').subscribe(function (res) {
            _this[container] = res.data;
        });
    };
    // 添加服务类目
    MainHeaderComponent.prototype.addAccountClass = function () {
        if (!this.sec) {
            return;
        }
        if (this.accountClass.length === 5) {
            this.type = 'info';
            this.message = '服务类目最多只能添加五个';
            this.isShow = true;
            return;
        }
        var firIdx = this.fir.indexOf('&');
        var secIdx = this.sec.indexOf('&');
        var firId = this.fir.substring(0, firIdx), secId = this.sec.substring(0, secIdx), firName = this.fir.substring(firIdx + 1), secName = this.sec.substring(secIdx + 1);
        this.accountClass.push({ firId: firId, secId: secId, firName: firName, secName: secName });
        this.fir = 0;
        this.sec = 0;
    };
    // 修改账户信息后提交
    MainHeaderComponent.prototype.accountEditSubmit = function () {
        var _this = this;
        this.accountExtend.company_address_code = this.regionCode + '';
        this.accountInfo.address_code_explain = this.provinceName + ' ' + this.cityName + ' ' + this.regionName;
        this.accountInfo.address_code = this.accountExtend.company_address_code;
        this.accountInfo.address_detail = this.accountExtend.company_address_detail;
        var arr = [];
        this.accountClass.every(function (val, idx, array) {
            arr.push(val.secId);
            return true;
        });
        this.accountExtend.class = JSON.stringify(arr);
        if (!this.accountExtend.contacts_email) {
            delete this.accountExtend.contacts_email;
        }
        var url = api_1.adSystemApiUrl + "/v1/member/account/edit";
        return this.httpService.httpPostAuth(url, this.accountInfo).subscribe(function (res) {
            if (res.code === 2000) {
                var url_1 = api_1.adSystemApiUrl + "/v1/member/account/editAccountExtend";
                return _this.httpService.httpPostAuth(url_1, _this.accountExtend).subscribe(function (rs) {
                    if (rs.code === 2000) {
                        if (_this.hasChangedImg) {
                            var id = _this.localStorageService.getUserInfo().id;
                            _this.adOssUploadService.getSignature('/v1/common/member_avatar/' + id + '?callback=true').subscribe(function (res1) {
                                _this.adOssUploadService.fileUpload(_this.img_file, res1.data, _this.img_name, 'avatar').subscribe(function (res2) {
                                    if (res2.status == 200) {
                                        _this.type = 'success';
                                        _this.message = '修改成功';
                                        _this.isShow = true;
                                        _this.personModal.hide();
                                        _this.getUserInfo();
                                    }
                                    else {
                                        _this.type = 'error';
                                        _this.message = '头像上传失败';
                                        _this.isShow = true;
                                    }
                                });
                            });
                        }
                        else {
                            _this.type = 'success';
                            _this.message = '修改成功';
                            _this.isShow = true;
                            _this.personModal.hide();
                            _this.getUserInfo();
                        }
                    }
                    else {
                        _this.type = 'error';
                        _this.message = rs.message;
                        _this.isShow = true;
                    }
                });
            }
            else {
                _this.type = 'error';
                _this.message = res.message;
                _this.isShow = true;
            }
        });
    };
    // 修改账户密码后提交
    MainHeaderComponent.prototype.passwordEditSubmit = function () {
        var _this = this;
        this.canClick = false;
        var url = api_1.adSystemApiUrl + "/v1/member/account/reset_password";
        return this.httpService.httpPostAuth(url, this.passwordInfo).subscribe(function (res) {
            _this.canClick = true;
            if (res.code === 2000) {
                _this.type = 'success';
                _this.message = '修改成功';
                _this.isShow = true;
                _this.personModal.hide();
                _this.getUserInfo();
                _this.passwordInfo.old_password = '';
                _this.passwordInfo.new_password = '';
                _this.passwordInfo.again_password = '';
            }
            else {
                if (!!res.data) {
                    _this.type = 'error';
                    _this.message = res.data.again_password[0];
                    _this.isShow = true;
                }
                else {
                    _this.type = 'error';
                    _this.message = res.message;
                    _this.isShow = true;
                }
            }
        });
    };
    MainHeaderComponent.prototype.getAddressDetail = function (code) {
        var _this = this;
        if (!!code) {
            this.addressService.getAddressDetail(code).subscribe(function (res) {
                if (res.data) {
                    if (res.data.province) {
                        _this.provinceCode = res.data.province.code;
                        _this.provinceName = res.data.province.name;
                        _this.getCityList(res.data.province.code);
                    }
                    if (res.data.city) {
                        _this.cityCode = res.data.city.code;
                        _this.cityName = res.data.city.name;
                        _this.getRegionList(res.data.city.code);
                    }
                    if (res.data.region) {
                        _this.regionName = res.data.region.name;
                        _this.regionCode = res.data.region.code;
                    }
                }
                _this.getProvinceList();
            });
        }
        else {
            this.getProvinceList();
        }
    };
    MainHeaderComponent.prototype.getProvinceList = function () {
        var _this = this;
        this.addressService.getProvinceList().subscribe(function (res) {
            if (res.code === 2000) {
                _this.provinceList = res.data;
            }
            else {
                _this.type = 'error';
                _this.message = res.message;
                _this.isShow = true;
            }
        });
    };
    MainHeaderComponent.prototype.getCityList = function (provinceCode) {
        var _this = this;
        this.addressService.getCityList(provinceCode).subscribe(function (res) {
            if (res.code === 2000) {
                _this.cityList = res.data;
            }
            else {
                _this.type = 'error';
                _this.message = res.message;
                _this.isShow = true;
            }
        });
    };
    MainHeaderComponent.prototype.getRegionList = function (cityCode) {
        var _this = this;
        this.addressService.getRegionList(cityCode).subscribe(function (res) {
            if (res.code === 2000) {
                _this.regionList = res.data;
            }
            else {
                _this.type = 'error';
                _this.message = res.message;
                _this.isShow = true;
            }
        });
    };
    // 选择省之后
    MainHeaderComponent.prototype.changeProvince = function () {
        var _this = this;
        this.cityCode = 0;
        this.regionCode = 0;
        this.regionList = [];
        this.provinceList.every(function (val, idx, array) {
            if (val.code == _this.provinceCode) {
                _this.provinceName = val.name;
                return false;
            }
            return true;
        });
        this.addressService.getCityList(this.provinceCode).subscribe(function (res) {
            if (res.code === 2000) {
                _this.cityList = res.data;
            }
            else {
                _this.type = 'error';
                _this.message = res.message;
                _this.isShow = true;
            }
        });
    };
    // 选择市之后
    MainHeaderComponent.prototype.changeCity = function () {
        var _this = this;
        this.regionCode = 0;
        this.cityList.every(function (val, idx, array) {
            if (val.code == _this.cityCode) {
                _this.cityName = val.name;
                return false;
            }
            return true;
        });
        this.addressService.getRegionList(this.cityCode).subscribe(function (res) {
            if (res.code === 2000) {
                _this.regionList = res.data;
            }
            else {
                _this.type = 'error';
                _this.message = res.message;
                _this.isShow = true;
            }
        });
    };
    // 选择区之后
    MainHeaderComponent.prototype.changeRegion = function () {
        var _this = this;
        this.regionList.every(function (val, idx, array) {
            if (val.code == _this.regionCode) {
                _this.regionName = val.name;
                return false;
            }
            return true;
        });
    };
    __decorate([
        core_1.Input()
    ], MainHeaderComponent.prototype, "hasHeaderAdditional", void 0);
    __decorate([
        core_1.ViewChild('personModal')
    ], MainHeaderComponent.prototype, "personModal", void 0);
    MainHeaderComponent = __decorate([
        core_1.Component({
            selector: 'app-main-header',
            templateUrl: './main-header.component.html',
            styleUrls: ['./main-header.component.scss'],
            providers: [
                local_storage_service_1.LocalStorageService, address_service_1.AddressService
            ]
        })
    ], MainHeaderComponent);
    return MainHeaderComponent;
}());
exports.MainHeaderComponent = MainHeaderComponent;
